// ==UserScript==
// @name           PimpMyiGoogle v2
// @namespace      http://www.google.*
// @description    Change the custom iGoogle Themes
// @include        http://www.google.*
// @include        https://www.google.*
// ==/UserScript==

// Really the PimpMyiGoogle userscript with some heavy modifications by me and some
// Style code from Rockmaster's iGoogle Sidebar Collapse (http://userscripts.org/users/14120/scripts) 

// Shortcut to document.getElementById
function _gel(id){ return document.getElementById(id); }

// Load settings
sidevis=GM_getValue('sb_vis',false);
headvis=GM_getValue('hd_vis',false);

// Kill the new elnarge box
//    .v2enlargebox {display:none !important}
GM_addStyle(<><![CDATA[
  #tog_hd:hover{-moz-opacity:0.6 !important}
  #tog_sb:hover{-moz-opacity:0.6}
  .modbox, .modbox_e {margin-bottom:3px !important;}
  .uftl{padding-top:2px !important; padding-bottom:1px !important;}
  #footerwrap{display:none !important}
]]></>);

// Get important elements
doLoad();

function doLoad(){
  gbar=_gel('gbar');    // bar
  nobr=gbar.firstChild; // the nobr element inside which are the links
  more=nobr.lastChild.previousSibling.previousSibling; // locate the "more" link
  // hide the undelete box that shows up when removing a gadget
  ud=_gel('undel_box');  if(ud){ ud.style.position ="relative"; ud.style.zIndex ="9999"; }
  if(_gel('nhdrwrap')) _gel('nhdrwrap').style.display=(headvis?'':'none');
  if(_gel('col1')) _gel('col1').style.display=(sidevis?'':'none');

  // For me, I like to have the Scholar link along the top links
  // The same thing can be repeated for others (eg Reader, Photos)
  var p = document.createElement("a");p.className="gb1";p.innerHTML = "Scholar";
  p.href ="http://scholar.google.com/scholar?hl=en&tab=ws";
  p.addEventListener("click", schLink, false); // make the link 'live' (i.e. be able to search)
  nobr.insertBefore(p,more); // insert before the "more" link

  // Now finish adding the toggle links
  if(_gel('nhdrwrap')){
    _gel('nhdrwrap').style.height='100px';
    _gel('nhdrwrap').style.overflow='hidden';
    _gel('nhdrwrapsizer').style.height='65px';
    p = document.createElement("div");p.id='tog_hd';p.title='Toggle Header';
    with(p.style){
      width='0px'; height='0px';
      border='8px solid transparent';
      cssFloat='left'; cursor='pointer';
      marginTop='5px'; marginLeft='-3px'; marginRight='3px';
      borderTopStyle=(!headvis?'solid':'none');
      borderBottomStyle=(headvis?'solid':'none');
      MozBorderBottomColors='transparent blue';
      MozBorderTopColors='transparent blue';
      MozOpacity=0.3;
    }
    p.href="javascript:void(null);";p.addEventListener("click", toggle_head, false);
    nobr.insertBefore(p,nobr.firstChild);
  }

  if(_gel('col1')){
    p = document.createElement("div");p.id='tog_sb';p.title='Toggle Sidebar';
    with(p.style){
      width='0px'; height='0px';
      border='8px solid transparent'; 
      cssFloat='left'; cursor='pointer';
      marginTop='0px';marginLeft='-5px'; marginRight='5px';
      borderLeftStyle=(sidevis?'none':'solid');
      borderRightStyle=(!sidevis?'none':'solid');
      MozBorderRightColors='transparent blue';
      MozBorderLeftColors='transparent blue';
      MozOpacity=0.3;
    }
    p.href="javascript:void(null);";p.addEventListener("click", toggle_sidebar, false);
    nobr.insertBefore(p,nobr.firstChild);
  }

  
  // Below is the only portion of the original PimpMyiGoogle script
  // Min-max boxes for gadgets
  var links=new Array();
  var mod = document.getElementById('modules');
  if(mod==null) return;
  var a_s = mod.getElementsByTagName('div');
  for (i=0;i<a_s.length;i++){
    //if a is a new max bos
    if (a_s[i].id.match("_home_menu") ){
      //_home_menu
      var mx = document.createElement('a');
      var id = a_s[i].id.match(/[0-9]{1,3}/g);
      mx.href="javascript:void(0);";
      mx.style.position='absolute';
      mx.style.left='0px';
      mx.style.marginTop='3px';
      mx.style.float='right';
      mx.style.zIndex='9999';
      var mxs = document.createElement('span');
      mxs.className="athidden";
      mxs.innerHTML = "max";
      mx.appendChild(mxs);
      a_s[i].parentNode.firstChild.firstChild.style.marginLeft='12px';
      //a_s[i].parentNode.parentNode.style.marginBottom='2px';
      a_s[i].parentNode.insertBefore(mx,a_s[i].parentNode.firstChild);
      mx.setAttribute('rel', id);
      links.push(mx);
    }
  }

  // set the class and add eventHandler
  for (i = 0; i < links.length; i++) {
    var a = ('m_'+links[i].rel+'_b');
    links[i].className =(document.getElementById(a).style.display == "none" ? "maxbox" : "minbox");
    
    links[i].addEventListener ( 'click', 	
    function (){
      var e = 'm_'+this.rel+'_b';
      unsafeWindow._zm(''+this.rel+'','32');
      this.className = this.className == 'maxbox'?'minbox':'maxbox';					
    }, false);
  }
}

// Toggling function - header
function toggle_head() {
  headbar=_gel('nhdrwrap');
  if(headbar==null) return;
  d=headbar.style.display=='none'? d='':'none';
  headbar.style.display=d;
  headbar.nextSibling.nextSibling.style.display = d;
  if(_gel('footerwrap')) _gel('footerwrap').style.display = 'none';
  with(_gel('tog_hd').style){
    borderTopStyle=(d=='none'?'solid':'none');
    borderBottomStyle=(d=='none'?'none':'solid');
    //MozBorderBottomColors=(d=='none'?'transparent blue':'none');
    //MozBorderTopColors=(d=='none'?'none':'transparent blue');
  }
  GM_setValue('hd_vis',d=='');
  return false;
}

// Toggling function - sidebar
function toggle_sidebar() {
  sidebar=_gel('col1');
  if(sidebar==null) return;
  d=sidebar.style.display=='none'? d='':'none';
  sidebar.style.display=d;
  with(_gel('tog_sb').style){
    if(d=='none'){
      // no sidebar: left arrow
      borderLeftStyle='solid';
      borderRightStyle='none';
      MozBorderLeftColors='transparent blue';
    }else{
      // sidebar: right arrow
      borderLeftStyle='none';
      borderRightStyle='solid';
      MozBorderRightColors='transparent blue';
    }
  }
  GM_setValue('sb_vis',d=='');
  return false;  
}

// Function to make the Scholar link created above as a live link
function schLink() {
  var q='';
  if(document.getElementsByName("q").length>0) q=document.getElementsByName("q")[0].value;
  this.href ="http://scholar.google.com/scholar?hl=en&tab=ws&q="+escape(q);
}
